package com.greenhouse9.prog05.mypackage;

import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;

import java.util.Properties;
import java.util.Vector;

public class Model02 implements java.io.Serializable {

    private Vector<String> vec = null;
	private String value = null;

    Connection con = null;
    Statement stmt = null;
    ResultSet rs = null;
    Exception exception;

	public Model02(){
	}

	public void execute () throws Exception{
		try {
            Class.forName("com.mysql.jdbc.Driver");

            String jdbcurl = "jdbc:mysql://localhost/test";

            Properties props = new Properties ();
            props.put("user", "root");
            props.put("password", "root");
            props.put("useUnicode", "true");
            props.put("characterEncoding", "utf8");

            con = DriverManager.getConnection(jdbcurl, props);

            String sql = "SELECT author_code, author_name FROM m_author"
                       + " order by author_code";

            //StringBufferにするほうがいい。

            Statement stmt = con.createStatement();
            rs = stmt.executeQuery(sql);

            while( rs.next() ) {
                String author_code = rs.getString("author_code");
                String author_name = rs.getString("author_name");

                vec.addElement(author_code + ": " + author_name);
            }
		}
		catch(Exception ex){
			exception = ex;
		}
        finally {
            if ( rs   != null) rs.close();
            if ( stmt != null) stmt.close();
            if ( con  != null) con.close();
            if ( exception != null) throw exception;
        }
	}

	public void setValue(String value){
		this.value = value;
	}

	public Vector<String> getVec() {
        return vec;
    }

}
